package com.ssm.config;

import com.ssm.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class filter implements HandlerInterceptor {
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入拦截器");
        String authToken = request.getHeader("Authorization");
        String requestURI = request.getRequestURI();
        if (authToken != null) {
            authToken = authToken.replace("Bearer ", "");
        }
        if (requestURI != null) {
            requestURI = requestURI.replace("/SSM_water_war", "");
        }
        JwtTokenUtil jwtTokenUtil=new JwtTokenUtil();
        String usernameFromToken = jwtTokenUtil.getUsernameFromToken(authToken);
        String tokenRedis = (String) redisTemplate.opsForValue().get(usernameFromToken);
        System.out.println("tokenRedis:"+tokenRedis);
        System.out.println("Authorization:"+authToken);
        System.out.println("requestURI:"+requestURI);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
